# 跳过所有编译器检查
set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_CXX_COMPILER_WORKS 1)
macro(__cmake_test_compiler)
endmacro()

cmake_minimum_required(VERSION 3.29)
project(stm32mpu_demo C CXX)
set(CMAKE_CXX_STANDARD 20)

# ================= 设置项目 ===============
file(GLOB_RECURSE MAIN_SRC
        # 选择对应的资源文件
        test_led.c
)


# ================== 处理逻辑 ==================
if(MAIN_SRC)
    list(GET MAIN_SRC 0 FIRST_SRC)
    get_filename_component(BASE_NAME ${FIRST_SRC} NAME_WE)

    # 分成两部分，用于区分版本
    if (${CMAKE_C_COMPILER} STREQUAL "/usr/bin/gcc")
        set(MODIFIED_NAME "${BASE_NAME}_x86")
        add_executable(${MODIFIED_NAME} ${MAIN_SRC})
    else ()
        include(arm-toolchain.cmake)
        # 以此处的情况，非主机GCC，那么就是ARM GCC
        message(STATUS "===== 检测到 ARM GCC 编译器 =====")
        add_executable(${BASE_NAME} ${MAIN_SRC})
    endif ()

else()
    message(FATAL_ERROR "未找到项目文件")
endif()

# 获取桌面路径（跨平台兼容）
if(WIN32)
    set(DESKTOP_DIR "$ENV{USERPROFILE}/Desktop")
elseif(UNIX AND NOT APPLE)
    set(DESKTOP_DIR "$ENV{HOME}/Desktop")
elseif(APPLE)
    set(DESKTOP_DIR "$ENV{HOME}/Desktop")
else()
    message(FATAL_ERROR "Unsupported platform")
endif()




if (NOT ${CMAKE_C_COMPILER} STREQUAL "/usr/bin/gcc")
    # 添加构建后复制命令
    add_custom_command(TARGET ${BASE_NAME} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy
            $<TARGET_FILE:${BASE_NAME}>  # 源文件：自动获取可执行文件路径
            "${DESKTOP_DIR}/${BASE_NAME}"  # 目标路径
            COMMENT "Copying executable to Desktop..."
    )
endif ()
